//
//  DataBase.m
//  Realm-demo
//
//  Created by 小菜 on 17/5/8.
//  Copyright © 2017年 蔡凌云. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "NoticeModel.h"
#import <Realm/Realm.h>

@interface DataBase : XCTestCase

@end

@implementation DataBase

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}


- (void)testExample {
    
    // 多个用户，每个用户拥有不同的数据库
//    [self setDefaultRealmUser:@"zhangsan"];
//    
//    [[RLMRealm defaultRealm] transactionWithBlock:^{
//        [[RLMRealm defaultRealm] addObject:[[NoticeModel alloc] initWithValue:@[@11,@"zhangsan"]]];
//    }];
}

- (void)testLisi {
    // 切换用户
//    [self setDefaultRealmUser:@"lisi"];
//    
//    [[RLMRealm defaultRealm] transactionWithBlock:^{
//        [[RLMRealm defaultRealm] addObject:[[NoticeModel alloc] initWithValue:@[@11,@"lisi"]]];
//    }];
}

- (void)testOnlyRead {
//    [self setDefaultRealmUser:@"lisi"];
//    // 设置李四的数据库为只读
//    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
//    config.readOnly = YES;
//    [RLMRealmConfiguration setDefaultConfiguration:config];
}

- (void)delect {
    // 删除数据库
    [self setDefaultRealmUser:@"lisi"];
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    NSArray<NSURL *> *realmFileURLS = @[
                                       config.fileURL,
                                       [config.fileURL URLByAppendingPathExtension:@"lock"],
                                       [config.fileURL URLByAppendingPathExtension:@"log_a"],
                                       [config.fileURL URLByAppendingPathExtension:@"log_b"],
                                       [config.fileURL URLByAppendingPathExtension:@"note"]
                                       ];
    for (NSURL *url in realmFileURLS) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
    }
    
}

- (void)setDefaultRealmUser:(NSString *)userName {
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    // 设置用户名数据库
    config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:userName] URLByAppendingPathExtension:@"realm"];
    [RLMRealmConfiguration setDefaultConfiguration:config];
}

@end
